﻿using System;
using System.Collections.Generic;

using RayDen.Library.Core.Primitives;
using RayDen.RayEngine.IntersectionEngines;

namespace PathEngine
{
    public class BvhGenerator : GenericBvhGenerator<RenderPrimitive>
    {
        protected FrameDataset dataset;

        public BvhGenerator(FrameDataset ds)
        {
            this.dataset = ds;
        }

        protected override AABB CalcBound(ref RenderPrimitive prim)
        {
            return dataset.Shapes[prim.Id].WorldBound;
        }
    }

    public class TopLevelAccellerationStructure
    {
        protected FrameDataset dataset;
        public List<RenderPrimitive> Primitives;
        public BvhNode[] topLevelBvh;

        public TopLevelAccellerationStructure(FrameDataset ds)
        {
            this.dataset = ds;
            this.Primitives = new List<RenderPrimitive>(ds.Primitives);
        }

        public void Add(RenderPrimitive prim)
        {
            this.Primitives.Add(prim);
        }

        public void Build()
        {
            var h = new BvhGenerator(this.dataset);
            h.BuildTree(this.Primitives, 0, this.Primitives.Count);
            this.topLevelBvh = h.nodes;
        }
    }
}
